/**
 * blog后台用户模块
 */
const express = require("express");
const { query, body } = require("express-validator");
const {
  checkUsername,
  login,
  logout,
  register,
  selectUserList,
  selectUser,
  insertUser,
  updateUser,
  updatePassword,
  deleteUser,
} = require("@root/services/blog/user.service.js");
const router = express.Router();

const parameters = {
  user_id: ([prop = "user_id", validator = body] = []) =>
    validator(prop).notEmpty().escape().withMessage("用户id不能为空"),
  nickname: ([prop = "nickname", validator = body] = []) =>
    validator(prop).notEmpty().escape().withMessage("昵称不能为空"),
  username: ([prop = "username", validator = body] = []) =>
    validator(prop).notEmpty().escape().withMessage("用户名不能为空"),
  password: ([prop = "password", validator = body] = []) =>
    validator(prop)
      .notEmpty()
      .escape()
      .withMessage("密码不能为空")
      .isLength({ min: 6 })
      .withMessage("密码长度至少为6位"),
  state: body("state").isIn([1, 2]).withMessage("评论状态只能是1 or 2"),
  sex: body("sex").notEmpty().escape().withMessage("性别不能为空"),
  age: body("age").notEmpty().escape().withMessage("年龄不能为空"),
  address: body("address").notEmpty().escape().withMessage("地址不能为空"),
  phone: body("phone").notEmpty().escape().withMessage("手机号不能为空"),
  email: body("email").notEmpty().escape().withMessage("邮箱不能为空"),
  avatar: body("avatar").notEmpty().escape().withMessage("头像不能为空"),
  role_id: body("role_id").notEmpty().escape().withMessage("角色不能为空"),
};

/**
 * 检查使用者名称
 */
router.get("/checkUsername", [parameters.username([, query])], checkUsername);

/**
 * 前台用户注册
 */
router.post(
  "/register",
  [
    parameters.nickname(),
    parameters.username(),
    parameters.password(),
    parameters.password(["verify_password"]),
    body().custom((value) => {
      const { password, verify_password } = value;
      if (password !== verify_password) {
        throw new Error("两次输入的密码不一致");
      }
      return true;
    }),
  ],
  register
);

/**
 * 用户登录
 */
router.post("/login", [parameters.username(), parameters.password()], login);

/**
 * 登出成功
 */
router.post("/logout", logout);

/**
 * 获取用户列表
 */
router.get("/list", selectUserList);

/**
 * 获取用户
 */
router.get("/", selectUser);

/**
 * 后台创建用户
 */
router.post("/", [parameters.username(), parameters.nickname()], insertUser);

/**
 * 修改用户
 */
router.put(
  "/",
  [
    parameters.user_id(),
    parameters.nickname(),
    parameters.sex,
    parameters.age,
    parameters.address,
    parameters.phone,
    parameters.email,
  ],
  updateUser
);

/**
 * 修改用户密码
 */
router.patch(
  "/password",
  [
    // parameters.user_id,
    parameters.username(),
    body("oldPassword").notEmpty().escape().withMessage("旧密码不能为空"),
    body("newPassword").notEmpty().escape().withMessage("新密码不能为空"),
    body().custom((value) => {
      const { oldPassword, newPassword } = value;
      if (oldPassword === newPassword) throw new Error("新密码不能和旧密码相同");
      return true;
    }),
  ],
  updatePassword
);

/**
 * 删除用户
 */
router.delete(
  "/",
  [
    body("ids").isArray({ min: 1 }).withMessage("ids必须是数组,且不能为空"),
    body("ids.*").isInt({ min: 1 }).withMessage("数组中的元素必须是正整数"),
  ],
  deleteUser
);

module.exports = router;
